<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="被链接的表ID" prop="linkedId">-->
<!--        <el-input-->
<!--          v-model="queryParams.linkedId"-->
<!--          placeholder="请输入被链接的表ID"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="被链接的表ID" prop="linkedId2">-->
<!--        <el-input-->
<!--          v-model="queryParams.linkedId2"-->
<!--          placeholder="请输入被链接的表ID"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="显示编号" prop="displayCode">
        <el-input v-model="queryParams.displayCode" placeholder="请输入显示编号"
          clearable @keyup.enter="handleQuery" maxlength="50"
        />
      </el-form-item>
      <el-form-item label="附件类型" prop="attachmentType" style="width: 220px">
        <el-select v-model="queryParams.attachmentType" placeholder="请选择附件类型" clearable >
          <el-option
            v-for="dict in scm_attachment_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="附件状态" prop="attachmentStatus" style="width: 160px">
        <el-select v-model="queryParams.attachmentStatus" placeholder="请选择附件状态" clearable>
          <el-option
            v-for="dict in scm_attachment_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['scm:scmAttachmentLinkage:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['scm:scmAttachmentLinkage:edit']"
        >修改</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="Delete"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['scm:scmAttachmentLinkage:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="Download"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['scm:scmAttachmentLinkage:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scmAttachmentLinkageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="${comment}" align="center" prop="id" />-->
<!--      <el-table-column label="被链接的表ID" align="center" prop="linkedId" />-->
<!--      <el-table-column label="被链接的表ID" align="center" prop="linkedId2" />-->
      <el-table-column label="显示编号" align="left" prop="displayCode" width="150"/>
      <el-table-column label="附件类型" align="center" prop="attachmentType" width="100">
        <template #default="scope">
          <dict-tag :options="scm_attachment_type" :value="scope.row.attachmentType"/>
        </template>
      </el-table-column>
      <el-table-column label="附件状态" align="center" prop="attachmentStatus" width="80">
        <template #default="scope">
          <dict-tag :options="scm_attachment_status" :value="scope.row.attachmentStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="名字" align="left" prop="attachmentName" />
<!--      <el-table-column label="上传下载" align="left" prop="attachmentUrl" >-->
<!--        <template #default="scope">-->
<!--          <span>{{ scope.row.attachmentUrl ? scope.row.attachmentUrl.substring(scope.row.attachmentUrl.lastIndexOf('/') + 1) : '' }}</span>-->
<!--          <el-button size="default" type="text" @click="$download.resource(scope.row.attachmentUrl,false)">下载</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
        <template #default="scope">
          <el-button size="default" icon="Bottom" type="text" @click="$download.resource(scope.row.attachmentUrl,false)">下载</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['scm:scmAttachmentLinkage:edit']">修改</el-button>
<!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['scm:scmAttachmentLinkage:remove']">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改全局附件对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="scmAttachmentLinkageRef" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="被链接的表ID" prop="linkedId">-->
<!--          <el-input v-model="form.linkedId" placeholder="请输入被链接的表ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="被链接的表ID" prop="linkedId2">-->
<!--          <el-input v-model="form.linkedId2" placeholder="请输入被链接的表ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="显示编号" prop="displayCode">
          <el-input v-model="form.displayCode" placeholder="请输入显示编号" disabled/>
        </el-form-item>
        <el-form-item label="附件类型" prop="attachmentType" >
          <el-select v-model="form.attachmentType" placeholder="请选择附件类型" disabled>
            <el-option
              v-for="dict in scm_attachment_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="附件状态" prop="attachmentStatus">
          <el-select v-model="form.attachmentStatus" placeholder="请选择附件状态">
            <el-option
              v-for="dict in scm_attachment_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名字" prop="attachmentName">
          <el-input v-model="form.attachmentName" placeholder="请输入附件名字" />
        </el-form-item>
        <el-form-item label="上传下载" prop="attachmentUrl">
          <div style="display: flex; gap: 10px; align-items: center; width: 100%">
            <el-input :value="form.attachmentUrl ? form.attachmentUrl.substring(form.attachmentUrl.lastIndexOf('/') + 1) : '' " placeholder="请上传图纸" style="flex: 2;" disabled />
<!--            <file-upload v-model="form.attachmentUrl" :isShowTip="false"/>-->
            <el-button size="default" type="text" @click="$download.resource(form.attachmentUrl,false)">下载</el-button>
          </div>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ScmAttachmentLinkage">
import { listScmAttachmentLinkage, getScmAttachmentLinkage, delScmAttachmentLinkage, addScmAttachmentLinkage, updateScmAttachmentLinkage } from "@/api/scm/scmAttachmentLinkage";

const { proxy } = getCurrentInstance();
const { scm_attachment_status, scm_attachment_type } = proxy.useDict('scm_attachment_status', 'scm_attachment_type');

const scmAttachmentLinkageList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    linkedId: null,
    linkedId2: null,
    linkedId3: null,
    displayCode: null,
    attachmentType: null,
    attachmentStatus: null,
  },
  rules: {
    linkedId: [
      { required: true, message: "被链接的表ID不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询全局附件列表 */
function getList() {
  loading.value = true;
  listScmAttachmentLinkage(queryParams.value).then(response => {
    scmAttachmentLinkageList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    linkedId: null,
    linkedId2: null,
    linkedId3: null,
    displayCode: null,
    attachmentType: null,
    attachmentStatus: null,
    attachmentName: null,
    attachmentUrl: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("scmAttachmentLinkageRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加全局附件";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getScmAttachmentLinkage(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改全局附件";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["scmAttachmentLinkageRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateScmAttachmentLinkage(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addScmAttachmentLinkage(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除全局附件编号为"' + _ids + '"的数据项？').then(function() {
    return delScmAttachmentLinkage(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('scm/scmAttachmentLinkage/export', {
    ...queryParams.value
  }, `scmAttachmentLinkage_${new Date().getTime()}.xlsx`)
}

getList();
</script>
